<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>写入文件（可安全用于二进制文件）</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.ftruncate.html">« ftruncate</a></li>
      <li style="float: right;"><a href="function.glob.html">glob »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.filesystem.html">文件系统函数</a></li>
    <li>写入文件（可安全用于二进制文件）</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.fwrite" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">fwrite</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">fwrite</span> &mdash; <span class="dc-title">写入文件（可安全用于二进制文件）</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.fwrite-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>fwrite</strong></span>(<span class="methodparam"><span class="type">resource</span> <code class="parameter">$stream</code></span>, <span class="methodparam"><span class="type">string</span> <code class="parameter">$data</code></span>, <span class="methodparam"><span class="type">?</span><span class="type"><span class="type">int</span><span class="type"></span></span> <code class="parameter">$length</code><span class="initializer"> = <strong><code>null</code></strong></span></span>): <span class="type"><span class="type">int</span>|<span class="type"><span class="type false">false</span></span></span></div>

  <p class="simpara">
   <span class="function"><strong>fwrite()</strong></span> 把 <code class="parameter">data</code> 的内容写入
   文件指针 <code class="parameter">stream</code> 处。 
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.fwrite-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">stream</code></dt>

     <dd>

      <p class="para">文件系统指针，是典型地由
<span class="function"><a href="function.fopen.html" class="function">fopen()</a></span> 创建的 <span class="type">resource</span>(资源)。</p>
     </dd>

    
    
     <dt>
<code class="parameter">data</code></dt>

     <dd>

      <p class="para">
       要写入的字符串。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">length</code></dt>

     <dd>

      <p class="para">
       如果 <code class="parameter">length</code> 是 <span class="type">int</span>，当写入了
       <code class="parameter">length</code> 个字节或者写完了 <code class="parameter">string</code>
       以后，写入就会停止，看先碰到哪种情况。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.fwrite-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="simpara">
   <span class="function"><strong>fwrite()</strong></span> 返回写入的字符数， 或者在失败时返回 <strong><code>false</code></strong>。
  </p>
 </div>

 
 <div class="refsect1 errors" id="refsect1-function.fwrite-errors">
  <h3 class="title">错误／异常</h3>
  <p class="para">
   <span class="function"><strong>fwrite()</strong></span> 失败时会引发 <strong><code>E_WARNING</code></strong>。
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.fwrite-changelog">
  <h3 class="title">更新日志</h3>
  <table class="doctable informaltable">
   
    <thead>
     <tr>
      <th>版本</th>
      <th>说明</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr>
      <td>8.0.0</td>
      <td>
       现在 <code class="parameter">length</code> 可以为 null。
      </td>
     </tr>

    </tbody>
   
  </table>

 </div>


 <div class="refsect1 examples" id="refsect1-function.fwrite-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-2189">
    <p><strong>示例 #1 简单的 <span class="function"><strong>fwrite()</strong></span> 示例</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$filename&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'test.txt'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$somecontent&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"Add&nbsp;this&nbsp;to&nbsp;the&nbsp;file\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;首先确认文件存在且可写。<br /></span><span style="color: #007700">if&nbsp;(</span><span style="color: #0000BB">is_writable</span><span style="color: #007700">(</span><span style="color: #0000BB">$filename</span><span style="color: #007700">))&nbsp;{<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;在示例中，使用追加模式打开&nbsp;$filename。<br />&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;文件指针应该在文件的末尾，<br />&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;那就是使用&nbsp;fwrite()&nbsp;写入&nbsp;$somecontent&nbsp;的地方。<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">if&nbsp;(!</span><span style="color: #0000BB">$fp&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #0000BB">$filename</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'a'</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Cannot&nbsp;open&nbsp;file&nbsp;(</span><span style="color: #0000BB">$filename</span><span style="color: #DD0000">)"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;将&nbsp;$somecontent&nbsp;写入到打开的文件。<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">if&nbsp;(</span><span style="color: #0000BB">fwrite</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$somecontent</span><span style="color: #007700">)&nbsp;===&nbsp;</span><span style="color: #0000BB">FALSE</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Cannot&nbsp;write&nbsp;to&nbsp;file&nbsp;(</span><span style="color: #0000BB">$filename</span><span style="color: #DD0000">)"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Success,&nbsp;wrote&nbsp;(</span><span style="color: #0000BB">$somecontent</span><span style="color: #DD0000">)&nbsp;to&nbsp;file&nbsp;(</span><span style="color: #0000BB">$filename</span><span style="color: #DD0000">)"</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">);<br /><br />}&nbsp;else&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"The&nbsp;file&nbsp;</span><span style="color: #0000BB">$filename</span><span style="color: #DD0000">&nbsp;is&nbsp;not&nbsp;writable"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.fwrite-notes">
  <h3 class="title">注释</h3>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <p class="para">
    Writing to a network stream may end before the whole string is written.
    Return value of <span class="function"><strong>fwrite()</strong></span> may be checked:
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">fwrite_stream</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$string</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(</span><span style="color: #0000BB">$written&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">;&nbsp;</span><span style="color: #0000BB">$written&nbsp;</span><span style="color: #007700">&lt;&nbsp;</span><span style="color: #0000BB">strlen</span><span style="color: #007700">(</span><span style="color: #0000BB">$string</span><span style="color: #007700">);&nbsp;</span><span style="color: #0000BB">$written&nbsp;</span><span style="color: #007700">+=&nbsp;</span><span style="color: #0000BB">$fwrite</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$fwrite&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fwrite</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">substr</span><span style="color: #007700">(</span><span style="color: #0000BB">$string</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$written</span><span style="color: #007700">));<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(</span><span style="color: #0000BB">$fwrite&nbsp;</span><span style="color: #007700">===&nbsp;</span><span style="color: #0000BB">false</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$written</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$written</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </p>
  </p></blockquote>
  
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <p class="para">
    在区分二进制文件和文本文件的系统上（如 Windows）
    打开文件时，<span class="function"><a href="function.fopen.html" class="function">fopen()</a></span> 函数的 mode 参数要加上 &#039;b&#039;。
   </p>
  </p></blockquote>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <p class="para">
    如果 <code class="parameter">stream</code> 是 <span class="function"><a href="function.fopen.html" class="function">fopen()</a></span>
    追加模式，<span class="function"><strong>fwrite()</strong></span> 是原子操作（unless the size of
    <code class="parameter">data</code> exceeds the filesystem&#039;s block size, on some
    platforms, and as long as the file is on a local filesystem）。也就是说，在调用
    <span class="function"><strong>fwrite()</strong></span> 之前不需要 <span class="function"><a href="function.flock.html" class="function">flock()</a></span>
    资源；所有的数据都将会不间断写入。
   </p>
  </p></blockquote>
    <blockquote class="note"><p><strong class="note">注意</strong>: 
   <p class="para">
    如果向文件指针写入两次，然后数据将追加到文件内容的末尾：
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$fp&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">'data.txt'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'w'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">fwrite</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'1'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">fwrite</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'23'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;“data.txt”的内容是&nbsp;123&nbsp;而不是&nbsp;23！<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </p>
  </p></blockquote>
 </div>

 
 <div class="refsect1 seealso" id="refsect1-function.fwrite-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.fread.html" class="function" rel="rdfs-seeAlso">fread()</a> - 读取文件（可安全用于二进制文件）</span></li>
    <li class="member"><span class="function"><a href="function.fopen.html" class="function" rel="rdfs-seeAlso">fopen()</a> - 打开文件或者 URL</span></li>
    <li class="member"><span class="function"><a href="function.fsockopen.html" class="function" rel="rdfs-seeAlso">fsockopen()</a> - 打开 Internet 或者 Unix 套接字连接</span></li>
    <li class="member"><span class="function"><a href="function.popen.html" class="function" rel="rdfs-seeAlso">popen()</a> - 打开进程文件指针</span></li>
    <li class="member"><span class="function"><a href="function.file-get-contents.html" class="function" rel="rdfs-seeAlso">file_get_contents()</a> - 将整个文件读入一个字符串</span></li>
    <li class="member"><span class="function"><a href="function.pack.html" class="function" rel="rdfs-seeAlso">pack()</a> - 将数据打包成二进制字符串</span></li>
   </ul>
  </p>
 </div>

 
</div></div></div></body></html>